define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'certification/user_agent/index' + location.search,
                    add_url: 'certification/user_agent/add',
                    edit_url: 'certification/user_agent/edit',
                    multi_url: 'certification/user_agent/multi',
                    import_url: 'certification/user_agent/import',
                    deal_url:  'order/order/index?uid={uid}',
                    toup_url:  'financial/user_order/index?uid={uid}',
                    table: 'user_agent',
                    dragsort_url: false,
                }
            });

            var table = $("#table");

            table.on('post-common-search.bs.table',function(e ,t){
                var link_obj = $("ul.nav-tabs li a[data-toggle='tab']").first();
                link_obj.parent("li").addClass("active");
                $("select[name='status']", t.$commonsearch).val(link_obj.data("value"));
                $("li[role='menuitem']:has(input[data-field='status'])").hide();
                $('label[for="status"]').parent().css('display','none');
                $("#table").bootstrapTable('hideColumn','status');
            });

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                searchFormVisible: true,
                commonSearch: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'user_name', title: __('User_name'), operate: 'LIKE'},
                        {field: 'province', title: __('Province'), operate: 'LIKE',visible:false},
                        {field: 'city', title: __('City'), operate: 'LIKE',visible:false},
                        {field: 'shop_name', title: __('Shop_name'), operate: 'LIKE'},
                        {field: 'online_shop', title: __('Online_shop'), operate: false},
                        {field: 'price_num', title: __('Price_num'), operate:false},
                        {field: 'trading_sum', title: __('Trading_sum'),operate:false},
                        {field: 'goods_num', title: __('Goods_num'),operate:false},
                        {field: 'user.money', title: __('Deposit_price'), operate:false},
                        {field: 'phone', title: __('Phone'), operate:'LIKE',visible:false},
                        {field: 'blackwitch', title: __('Blackwitch'),operate:false, searchList: {"1": '加入黑名单', '0': '移除黑名单'}, table: table, formatter: Table.api.formatter.toggle},
                        {field: 'is_oneagent', title: __('is_oneagent'),operate:false, searchList: {"1": '一级代理', '0': '移除一级代理'}, table: table, formatter: Table.api.formatter.toggle},
                        {field: 'status', title: __('Status'),visible:false, searchList: {"0":__('Status 0'),"1":__('Status 1'),"2":__('Status 2')}, formatter: Table.api.formatter.status},
                        {field: 'operate', title: __('Operate'), table: table,
                            events: Table.api.events.operate,
                            cellStyle: function(){
                                return { css:{'max-width':'80px',"white-space":"pre-line",'white-space':'pre-wrap'}};
                            },
                            buttons :[
                            {
                                name: 'edit',
                                text: '审核',
                                classname : 'btn btn-xs btn-primary btn-dialog',
                                url : $.fn.bootstrapTable.defaults.extend.edit_url,
                                refresh: true,
                                visible : function(row){
                                    if (row.status == 1)
                                    {
                                        return false;
                                    }else{
                                        return true;
                                    }
                                }
                            },{
                                    name: 'show',
                                    text: '详情',
                                    classname : 'btn btn-xs btn-primary btn-dialog',
                                    icon: 'fa fa-list',
                                    extend: 'data-area=\'["100%", "100%"]\'',
                                    url: 'certification/user_agent/show',
                                    refresh: true,
                                    visible: function (row){
                                        if (row.status == 0)
                                        {
                                            return false;
                                        }else{
                                            return true;
                                        }
                                    }
                                },{
                                    name: 'deal',
                                    text: '交易记录',
                                    classname : 'btn btn-xs btn-primary btn-dialog',
                                    icon: 'fa fa-list',
                                    extend: 'data-area=\'["100%", "100%"]\'',
                                    url : $.fn.bootstrapTable.defaults.extend.deal_url,
                                    visible : true
                                },{
                                    name: 'toup',
                                    text: '充值记录',
                                    extend: 'data-area=\'["100%", "100%"]\'',
                                    classname : 'btn btn-xs btn-primary btn-dialog',
                                    icon: 'fa fa-list',
                                    url : $.fn.bootstrapTable.defaults.extend.toup_url,
                                    visible : true
                                }
                        ],
                            formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        show: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                $(".layer-close").click(function () {
                    var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                    parent.layer.close(index); //再执行关闭
                })

                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
